<?php
namespace app\api\controller\customer;

use app\api\controller\Base;
use app\api\controller\BaseUser;
use app\common\model\company\Company;
use app\common\model\user\User;
use think\facade\Request;
use app\common\model\customer\CustomerProjectType;
use app\common\model\customer\CustomerProject;
use app\common\model\customer\Customer;
//客户项目
class Project extends BaseUser
{
    /**
     * 获取项目分类
     * @return [type] [description]
     */
	public function getcreateinfo(){
        $data['type'] = CustomerProjectType::getcreatelist();
        $data['state'] = CustomerProject::distinct(true)->column('State');
        return myok($data);
    }
    /**
     * 创建项目信息
     * @return [type] [description]
     */
    public function createinfo(){
        $data = input('post.');
        if(empty($data['CustomerId'])){
            return myerr('缺少客户信息，请重新尝试');
        }
        //修改字段验证
        self::data_vali($data);
        $res = CustomerProject::screate($data['CustomerId'],$data['ProjectTypeId'],$data);
        return myok('登记成功');   
    }
    /**
     * 获取项目需要的修改信息
     * @return [type] [description]
     */
    public function getreinfo(){
        $id = input('id');
        if(empty($id)){
            return myerr('缺少项目id');
        }
        $data = CustomerProject::get($id);
        if(empty($data)){
            return myerr('没找到您需要的项目信息','go_back');
        }
        $data['type'] = CustomerProjectType::getcreatelist();
        $data['state'] = CustomerProject::distinct(true)->column('State');
        return myok($data);
    }
    /**
     * 修改项目信息
     * @return [type] [description]
     */
    public function reinfo(){
        $data = input('post.');
        $id = input('Id');
        if(empty($id)){
            return myerr('缺少项目id');
        }
        //修改字段验证
        self::data_vali($data);
        //获取项目信息
        $model = CustomerProject::get($id);
        if(empty($model)){
            return myerr('没找到信息');
        }
        //客户守卫 判断是否有权限查看
        Customer::run_guard($model->CustomerId,$this->user_id);
        //修改客户信息
        $res = CustomerProject::reinfo($model,$data['ProjectTypeId'],$data);
        return myok($res);
    }

    /**
     * 删除项目
     * @return [type] [description]
     */
    public function deleteinfo(){
        $id = input('id');
        if(empty($id)){
            return myerr('缺少项目id');
        }
        //获取项目信息
        $model = CustomerProject::get($id);
        if(empty($model)){
            return myerr('没找到信息');
        }
        //客户守卫 判断是否有权限查看
        Customer::run_guard($model->CustomerId,$this->user_id);
        //删除客户信息
        $res = CustomerProject::deleteinfo($model);
        return myok($res);
    }

    /**
     * 数据验证
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    private function data_vali($data){
        $file = ['Name','Location','ProjectTypeId','State'];
        $msg = ['Name'=>'输入项目名称','Location'=>'输入项目地址','ProjectTypeId'=>'选择项目分类','State'=>'输入项目状态'];
        foreach ($file as $key => $value) {
            if(empty($data[$value])){
                return moerr('请'.$msg[$value]);
            }
        }
    }
}
